package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Category;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Responser;

/**
 * Created by ZabbarovMZ on 22.04.2014.
 */
public class MenuCategoryWidget extends Composite implements
    Responser,HasValueChangeHandlers<JSONArray>, HasOpenHandlers<Category> {

    interface MenuCategoryWidgetUiBinder extends UiBinder<Widget, MenuCategoryWidget> { }
    private static MenuCategoryWidgetUiBinder uiBinder = GWT.create(MenuCategoryWidgetUiBinder.class);

    @UiField  protected MenuCategoryControl categoryControl;
    @UiField  protected AbsolutePanel dragSpace;
    private MenuCategoryContainer categoryContainer;
    private MenuCategoryBox selectedBox;

    @UiFactory
    MenuCategoryControl makeMenuContentControl() { // method name is insignificant
        return new MenuCategoryControl();
    }


    public MenuCategoryWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        this.categoryContainer = new MenuCategoryContainer(dragSpace);
        this.categoryControl.addButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                addNewItem();
            }
        });
        this.categoryControl.editButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                editSelectedItem(selectedBox);
            }
        });
        this.categoryControl.removeButton.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                deleteSelectedItem(selectedBox);
                categoryControl.setItemSelectedMode(false);
            }
        });
        this.categoryContainer.addSelectionHandler (new SelectionHandler<MenuCategoryBox>() {
            @Override
            public void onSelection(SelectionEvent<MenuCategoryBox> event) {
                selectedBox = event.getSelectedItem();
                categoryControl.setItemSelectedMode(true);
            }
        });
        this.categoryContainer.addValueChangeHandler(new ValueChangeHandler<JSONArray>() {
            @Override
            public void onValueChange(ValueChangeEvent<JSONArray> event) {
                saveItemsOrder(event.getValue());
            }
        });
    }

    @Override
    public HandlerRegistration addOpenHandler(OpenHandler<Category> handler) {
        return this.categoryContainer.addOpenHandler(handler);
    }

    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<JSONArray> handler) {
        return super.addHandler(handler, ValueChangeEvent.getType());
    }

    @Override
    public void handleGetResponse(JSONArray data) {
        selectedBox = null;
        categoryControl.setItemSelectedMode(false);
        categoryContainer.setData(data);
        ValueChangeEvent.fire(this, data);
    }
    @Override
    public void handlePostResponse(int responseCode) {
        if (200 == responseCode) {
            Requester request = new Requester();
            request.addResponser(this);
            request.getCategoryAll();
        }
    }

    public void prepare() {
        this.categoryContainer.clear();
        this.categoryControl.setItemSelectedMode(false);
        Requester request = new Requester();
        request.addResponser(this);
        request.getCategoryAll();
    }

    private void saveItemsOrder(JSONArray data) {
        Requester request = new Requester();
        request.setCategoryAll(data);
        ValueChangeEvent.fire(this, data);
    }

    private void addNewItem() {
        Category newCategory = new Category();
        MenuCategoryPopup popup = new MenuCategoryPopup(newCategory);
        popup.addValueChangeHandler(new ValueChangeHandler<Category>() {
            @Override
            public void onValueChange(ValueChangeEvent<Category> event) {
                Category newValue = event.getValue();
                // send post request to server
                Requester request = new Requester();
                request.addResponser(MenuCategoryWidget.this);
                request.setCategoryOne(newValue.getJSON());
                // TODO show process-glass
            }
        });
    }

    private void editSelectedItem(MenuCategoryBox selectedBox) {
        if (selectedBox != null) {
            MenuCategoryPopup popup = new MenuCategoryPopup(selectedBox.getCategory());
            popup.addValueChangeHandler(new ValueChangeHandler<Category>() {
                @Override
                public void onValueChange(ValueChangeEvent<Category> event) {
                    Category newValue = event.getValue();
                    // send post request to server
                    Requester request = new Requester();
                    request.addResponser(MenuCategoryWidget.this);
                    request.setCategoryOne(newValue.getJSON());
                    // TODO show process-glass
                }
            });
        }
    }

    private void deleteSelectedItem(MenuCategoryBox selectedBox) {
        if (selectedBox != null) {
            // send post request to server
            Requester request = new Requester();
            request.addResponser(MenuCategoryWidget.this);
            request.deleteCategoryById(selectedBox.getCategoryId());
            // TODO show process-glass
        }
    }
}
